import { defineStore } from 'pinia'
import { reactive } from 'vue'
// 使用vueuse 将数据存储本地
import { useStorage } from '@vueuse/core'
import {getUserInfo} from '@/api/user'

export const useUserStore = defineStore('user', () => {
  const user = reactive({
		accountId:'',
	  username:'',
	  avatar:'',
	  token:useStorage('TOKEN',null),
	  roles:[]
  })
	
  function setUserToken(token){
		user.token = token
  }
	
  function getInfo(){
	  return new Promise(async (resolve,reject)=>{
		  await getUserInfo()
		  .then((resp)=>{
			  //设置角色
			  user.roles  = resp.data.userInfo.roleName
			  user.accountId  = resp.data.userInfo.accountId
			  user.username  = resp.data.userInfo.username
			  user.avatar  = resp.data.userInfo.avatar
			  resolve(resp.data)
		  },(error)=>{
			  reject(error)
		  })
	  })
  }
  return { user,setUserToken,getInfo,}
})